/*
 * @Author: Cao Shixin
 * @Date: 2021-01-04 17:54:49
 * @LastEditors: Cao Shixin
 * @LastEditTime: 2021-01-04 18:16:18
 * @Description: 
 */

import 'dart:async';

import 'package:android_id/android_id.dart';
import 'package:flutter/services.dart';

import 'model/android_device_info.dart';
import 'model/ios_device_info.dart';

export 'model/android_device_info.dart';
export 'model/ios_device_info.dart';

class DeviceInfoEnhanced {
  static const MethodChannel _channel = const MethodChannel('device_info_csx');
  static AndroidDeviceInfo? _androidDeviceInfo;

  // Method channel for Android devices
  static Future<AndroidDeviceInfo> androidInfo() async {
    var androidInfo = _androidDeviceInfo;
    if (androidInfo == null) {
      var map = (await _channel
              .invokeMapMethod<String, dynamic>('getAndroidDeviceInfo')) ??
          <String, dynamic>{};
      var androidId = await AndroidId().getId();
      map['androidId'] = androidId;
      var info = AndroidDeviceInfo.fromMap(map);
      _androidDeviceInfo = info;
      return info;
    } else {
      return androidInfo;
    }
  }

  // Method channel for iOS devices
  static Future<IosDeviceInfo> iosInfo() async {
    return IosDeviceInfo.fromMap(
      (await _channel.invokeMethod('getIosDeviceInfo')).cast<String, dynamic>(),
    );
  }
}
